package com.qf.produce;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;


public class MQProducer {
    public static void main(String[] args) throws Exception {
        //发送消息的步骤
        //1.创建消息生产者producer(DefaultMQProducer)，并制定生产者group
        DefaultMQProducer producer = new DefaultMQProducer("test-group");
        //2.指定Nameserver地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动producer
        producer.start();
        //4.创建Message对象，指定主题Topic、消息体
        Message message = new Message("test-topicB", "test0tagB", "你相信光嘛666666666666".getBytes());
        //5.发送消息
        SendResult send = producer.send(message);
        System.out.println(send);
        //6.关闭生产者producer
        producer.shutdown();


//        DefaultMQProducer producer = new DefaultMQProducer("test-group");
//        producer.setNamesrvAddr("localhost:9876");
//        producer.start();
//        Message message = new Message("test-topic","test0tag","你好 异步mq".getBytes(StandardCharsets.UTF_8));
//        producer.send(message, new SendCallback() {
//            @Override
//            public void onSuccess(SendResult sendResult) {
//                System.out.println(sendResult);
//                producer.shutdown();
//            }
//
//            @Override
//            public void onException(Throwable throwable) {
//                throwable.printStackTrace();
//                producer.shutdown();
//            }
//        });



//        DefaultMQProducer producer = new DefaultMQProducer("test-group");
//        producer.setNamesrvAddr("localhost:9876");
//        producer.start();
//        Message message = new Message("test-topic2222","test0tag","单向mq".getBytes());
//        System.out.println(message);
//        producer.setRetryTimesWhenSendFailed(5);
//        producer.sendOneway(message);


    }
}
